app.controller("homeVersionCtrl", function ($scope, $rootScope, $http) {
    $rootScope.activeSidebar = Class.getConstant('SIDEBAR_VERSION');
    $scope.editValue = "编辑";
    $scope.isEdit = false;
    $scope.getVersion = function () {
        var url = $rootScope.rootUrl + Http.GetVersion;
        $http({method: "POST", url: url}).then(function (data) {
            console.log(data);
            $scope.version = data.data.resultMap.data;
            $scope.minSupportVersion = $scope.version.minSupportVersion;
            $scope.currentVersion = $scope.version.currentVersion;
            $scope.versionName = $scope.version.versionName;
            $scope.updateContent = $scope.version.updateContent;
            $scope.fileName = $scope.version.apkUrl;
        });
    };
    $scope.getVersion();
    $scope.edit = function () {
        console.log("edit");
        if ($scope.editValue === "编辑") {
            $scope.editValue = "确定";
            $scope.isEdit = true;
        } else {
            $scope.editValue = "编辑";
            $scope.isEdit = false;

            //更新版本信息
            var callback = function (data) {
                console.log(data);
                if (data.data.status = 1000) {
                    alert("修改成功");
                    $scope.getVersion();
                }
                else
                    alert("修改失败");
            };
            var url = $rootScope.rootUrl + Http.UpdateVersionMsg;
            var jsonobj = {
                CurrentVersion: $scope.currentVersion,
                MinSupportVersion: $scope.minSupportVersion,
                VersionName: $scope.versionName,
                UpdateContent: $scope.updateContent
            };
            $http({method: "POST", url: url, data: jsonobj}).then(callback);
            upload();
        }
    };
    $scope.quit = function () {
        console.log("quit");
        $scope.editValue = "编辑";
        $scope.isEdit = false;

        $scope.minSupportVersion = $scope.version.minSupportVersion;
        $scope.currentVersion = $scope.version.currentVersion;
        $scope.versionName = $scope.version.versionName;
        $scope.updateContent = $scope.version.updateContent;
        $scope.fileName = $scope.version.apkUrl;
    };


    function upload() {
        //确认信息，上传服务器
        var url = $rootScope.rootUrl + Http.UpdateApk;//接口路径
        var fd = new FormData();
        // var file = document.querySelector('input[type=file]').files[0];
        if ($scope.file) {
            console.log($scope.file[0]);
        } else {
            // alert('$scope.file 为空！');
            return;
        }
        fd.append($scope.file[0].name, $scope.file[0]);//参数 testReport=后台定义上传字段名称 ； $scope.file[0] 内容
        $http.post(url, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        }).success(function (data) {
            console.log(data);
            if (data.status != 1000) {
                alert(JSON.stringify('文件导入失败：' + $scope.file[0].name + '，请重新上传正确的文件或格式'));
            } else {
                $scope.refreshHomeAdImgList();
                alert(JSON.stringify('文件导入成功：' + $scope.file[0].name));
            }
            //恢复按钮
            $scope.imgDisabled = false;
            $scope.submitDisabled = false;
        }).error(function (data) {
            alert('服务器错误，文件导入失败！');
            //恢复按钮
            $scope.imgDisabled = false;
            $scope.submitDisabled = false;
        });
    }
});
//文件上传
app.directive("fileUpload", function () {
    return {
        //通过设置项来定义
        restrict: 'AE',
        scope: false,
        template: '<div class="fl"><input type="button" id="storeBtn" style="padding:0; position: absolute; top: 0; left: 0; background: none; border: none;color: #fff; width:84px; height: 34px; line-height: 34px;" value="选择文件"><input type="file" name="testReport" id="file" ng-disabled="imgDisabled" style="position: absolute; top: 0; left: 0; opacity: 0; width:84px; height: 34px;" accept=".jpg,.png,.apk,.jar,.war,.rar,.zip"></div>', //name:testReport 与接口字段相对应。
        replace: true,
        link: function (scope, ele, attrs) {
            ele.bind('click', function () {
                console.log("fileUpload click");
                $('#file').val('');
            });
            ele.bind('change', function () {
                console.log("fileUpload change");
                scope.file = ele[0].children[1].files;
                if (scope.file[0].size > 52428800) {
                    alert("大小不大于50M");
                    scope.file = null;
                    return false;
                }
                scope.fileName = scope.file[0].name;
                var postfix = scope.fileName.substring(scope.fileName.lastIndexOf(".") + 1).toLowerCase();
                // if (postfix != "jpg" && postfix != "png") {
                //     alert("图片仅支持png、jpg类型的文件");
                //     scope.fileName = "";
                //     scope.file = null;
                //     scope.$apply();
                //     return false;
                // }
                scope.$apply();
                scope.reader = new FileReader(); //创建一个FileReader接口
                console.log(scope.reader);
                console.log(scope.file);
                if (scope.file) {
                    //获取图片（预览图片）
                    scope.reader.readAsDataURL(scope.file[0]); //FileReader的方法，把图片转成base64
                    scope.reader.onload = function (ev) {
                        scope.$apply(function () {
                            scope.thumb = {
                                imgSrc: ev.target.result  //接收base64，scope.thumb.imgSrc为图片。
                            };
                        });
                    };
                } else {
                    alert('上传不能为空!');
                }
            });
        }
    };
});